package com.example.keyboardedittext.keyboard;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.example.keyboardedittext.R;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by gongshuobo on 2016/11/18.
 * 语文键盘的View
 */

public class KeyBoardChineseView extends LinearLayout {

    /**
     * 语文键盘三种小类型(字母,符号,声调)
     **/

    private View chineseSignView;
    private View chineseToneView;
    private LinearLayout chineseLetterCaseView;
    private LinearLayout chineseLetterSmallView;
    private LinearLayout chineseView;

    /**
     * 导航栏
     */

    private TextView chineseNavigationSign;
    private TextView chineseNavigationTone;
    private TextView chineseNavigationLetter;



    /**
     * 语文键盘小写字母对应的id和值映射
     **/
    private int[] chineseLetterSmallKey = {R.id.tv_math_letter_a, R.id.tv_math_letter_b, R.id.tv_math_letter_c, R.id.tv_math_letter_d, R.id.tv_math_letter_e, R.id.tv_math_letter_f, R.id.tv_math_letter_g, R.id.tv_math_letter_h, R.id.tv_math_letter_i, R.id.tv_math_letter_j, R.id.tv_math_letter_k, R.id.tv_math_letter_l, R.id.tv_math_letter_m, R.id.tv_math_letter_n, R.id.tv_math_letter_o, R.id.tv_math_letter_p, R.id.tv_math_letter_q, R.id.tv_math_letter_r, R.id.tv_math_letter_s, R.id.tv_math_letter_t, R.id.tv_math_letter_u, R.id.tv_math_letter_v, R.id.tv_math_letter_w, R.id.tv_math_letter_x, R.id.tv_math_letter_y, R.id.tv_math_letter_z};
    private int[] chineseLetterSmallValue = {R.string.keyboard_sign_a, R.string.keyboard_sign_b, R.string.keyboard_sign_c, R.string.keyboard_sign_d, R.string.keyboard_sign_e, R.string.keyboard_sign_f, R.string.keyboard_sign_g, R.string.keyboard_sign_h, R.string.keyboard_sign_i, R.string.keyboard_sign_j, R.string.keyboard_sign_k, R.string.keyboard_sign_l, R.string.keyboard_sign_m, R.string.keyboard_sign_n, R.string.keyboard_sign_o, R.string.keyboard_sign_p, R.string.keyboard_sign_q, R.string.keyboard_sign_r, R.string.keyboard_sign_s, R.string.keyboard_sign_t, R.string.keyboard_sign_u, R.string.keyboard_sign_v, R.string.keyboard_sign_w, R.string.keyboard_sign_x, R.string.keyboard_sign_y, R.string.keyboard_sign_z};
    /**
     * 语文键盘大写字母对应的id和值映射
     **/
    private int[] chineseLetterCaseKey = {R.id.tv_math_letter_A, R.id.tv_math_letter_B, R.id.tv_math_letter_C, R.id.tv_math_letter_D, R.id.tv_math_letter_E, R.id.tv_math_letter_F, R.id.tv_math_letter_G, R.id.tv_math_letter_H, R.id.tv_math_letter_I, R.id.tv_math_letter_J, R.id.tv_math_letter_K, R.id.tv_math_letter_L, R.id.tv_math_letter_M, R.id.tv_math_letter_N, R.id.tv_math_letter_O, R.id.tv_math_letter_P, R.id.tv_math_letter_Q, R.id.tv_math_letter_R, R.id.tv_math_letter_S, R.id.tv_math_letter_T, R.id.tv_math_letter_U, R.id.tv_math_letter_V, R.id.tv_math_letter_W, R.id.tv_math_letter_X, R.id.tv_math_letter_Y, R.id.tv_math_letter_Z};
    private int[] chineseLetterCaseValue = {R.string.keyboard_sign_capital_a, R.string.keyboard_sign_capital_b, R.string.keyboard_sign_capital_c, R.string.keyboard_sign_capital_d, R.string.keyboard_sign_capital_e, R.string.keyboard_sign_capital_f, R.string.keyboard_sign_capital_g, R.string.keyboard_sign_capital_h, R.string.keyboard_sign_capital_i, R.string.keyboard_sign_capital_j, R.string.keyboard_sign_capital_k, R.string.keyboard_sign_capital_l, R.string.keyboard_sign_capital_m, R.string.keyboard_sign_capital_n, R.string.keyboard_sign_capital_o, R.string.keyboard_sign_capital_p, R.string.keyboard_sign_capital_q, R.string.keyboard_sign_capital_r, R.string.keyboard_sign_capital_s, R.string.keyboard_sign_capital_t, R.string.keyboard_sign_capital_u, R.string.keyboard_sign_capital_v, R.string.keyboard_sign_capital_w, R.string.keyboard_sign_capital_x, R.string.keyboard_sign_capital_y, R.string.keyboard_sign_capital_z};

    /**
     * 语文键盘符号对应的id和映射
     */
    private int[] chineseSignKey = {R.id.tv_keyboard_sign_comma, R.id.tv_keyboard_sign_period, R.id.tv_keyboard_sign_question, R.id.tv_keyboard_sign_sigh, R.id.tv_math_number_colon, R.id.tv_keyboard_sign_semicolon, R.id.tv_math_number_point, R.id.tv_math_number_ellipsis, R.id.tv_keyboard_sign_last_quotes, R.id.tv_keyboard_sign_next_quotes, R.id.tv_keyboard_sign_last_single, R.id.tv_keyboard_sign_next_single, R.id.tv_keyboard_sign_left_book_name, R.id.tv_keyboard_sign_right_book_name, R.id.tv_math_division_left_bracket, R.id.tv_math_division_right_bracket, R.id.tv_keyboard_sign_horizontal_line, R.id.tv_keyboard_sign_dayton};
    private int[] chineseSignValue = {R.string.keyboard_sign_comma_chinese, R.string.keyboard_sign_period_chinese, R.string.keyboard_sign_question_chinese, R.string.keyboard_sign_sigh_chinese, R.string.keyboard_sign_collon_chinese, R.string.keyboard_sign_semicolon_chinese, R.string.keyboard_sign_point_chinese, R.string.keyboard_sign_apostrophe, R.string.keyboard_sign_last_quotes, R.string.keyboard_sign_next_quotes, R.string.keyboard_sign_last_single, R.string.keyboard_sign_next_single, R.string.keyboard_sign_left_book_name, R.string.keyboard_sign_right_book_name, R.string.keyboard_sign_left_bracket_chinese, R.string.keyboard_sign_right_bracket_chinese, R.string.keyboard_sign_horizontal_line, R.string.keyboard_sign_dayton};



    /**
     * 语文键盘声调对应的id和映射
     */

    private int[] chineseToneKey = {R.id.tv_keyboard_tone_ā, R.id.tv_keyboard_tone_á, R.id.tv_keyboard_tone_ă, R.id.tv_keyboard_tone_à, R.id.tv_keyboard_tone_ō, R.id.tv_keyboard_tone_ó, R.id.tv_keyboard_tone_ǒ, R.id.tv_keyboard_tone_ò, R.id.tv_keyboard_tone_ē, R.id.tv_keyboard_tone_é, R.id.tv_keyboard_tone_ě, R.id.tv_keyboard_tone_è, R.id.tv_keyboard_tone_ī, R.id.tv_keyboard_tone_í, R.id.tv_keyboard_tone_ĭ, R.id.tv_keyboard_tone_ì, R.id.tv_keyboard_tone_ū, R.id.tv_keyboard_tone_ú, R.id.tv_keyboard_tone_ǔ, R.id.tv_keyboard_tone_ù, R.id.tv_keyboard_tone_ǖ, R.id.tv_keyboard_tone_ǘ, R.id.tv_keyboard_tone_ǚ, R.id.tv_keyboard_tone_ǜ,};
    private int[] chineseToneValue = {R.string.keyboard_tone_ā, R.string.keyboard_tone_á, R.string.keyboard_tone_ă, R.string.keyboard_tone_à, R.string.keyboard_tone_ō, R.string.keyboard_tone_ó, R.string.keyboard_tone_ǒ, R.string.keyboard_tone_ò, R.string.keyboard_tone_ē, R.string.keyboard_tone_é, R.string.keyboard_tone_ě, R.string.keyboard_tone_è, R.string.keyboard_tone_ī, R.string.keyboard_tone_í, R.string.keyboard_tone_ĭ, R.string.keyboard_tone_ì, R.string.keyboard_tone_ū, R.string.keyboard_tone_ú, R.string.keyboard_tone_ǔ, R.string.keyboard_tone_ù, R.string.keyboard_tone_ǖ, R.string.keyboard_tone_ǘ,
            R.string.keyboard_tone_ǚ,
            R.string.keyboard_tone_ǜ,
    };

    /**
     * 语文键盘对应的Map键映射
     **/
    private Map<Integer, String> keyboardChineseMap;

    /**
     * 语文键盘实现输入完成接口
     **/
    private KeyBoardCallBack keyBoardCallBack;

    public KeyBoardChineseView(Context context) {
        super(context);
        initKeyBoardChineseView();
        initKeyBoardChineseMap();
        initKeyboardChineseLetterSmallOnClick();
        initKeyboardChineseLetterCaseOnClick();
    }

    private void initKeyboardChineseToneOnClick() {
        for(int chineseToneId:chineseToneKey){
            chineseToneView.findViewById(chineseToneId).setOnClickListener(keyboardChineseOnClick);
        }
        chineseToneView.findViewById(R.id.tv_keyboard_delete).setOnClickListener(keyboardChineseOnClick);
        chineseToneView.findViewById(R.id.tv_keyboard_delete).setOnLongClickListener(keyboardChineseOnLongClick);
    }

    private void initKeyboardChineseSignOnClick() {
        for(int chineseSignId:chineseSignKey){
            chineseSignView.findViewById(chineseSignId).setOnClickListener(keyboardChineseOnClick);
        }
        chineseSignView.findViewById(R.id.tv_keyboard_delete).setOnClickListener(keyboardChineseOnClick);
        chineseSignView.findViewById(R.id.tv_keyboard_delete).setOnLongClickListener(keyboardChineseOnLongClick);
    }


    /**
     * 默认添加一套语文小写字母键盘的映射
     **/
    private void initKeyBoardChineseMap() {
        keyboardChineseMap = new HashMap<Integer, String>();
        //语文小写字母键盘键值对
        for(int i=0;i<chineseLetterSmallKey.length;i++){
            keyboardChineseMap.put(chineseLetterSmallKey[i],getResources().getString(chineseLetterSmallValue[i]));
            keyboardChineseMap.put(chineseLetterCaseKey[i],getResources().getString(chineseLetterCaseValue[i]));
        }
        //语文符号键盘键值对
        for(int i=0;i<chineseSignKey.length;i++){
            keyboardChineseMap.put(chineseSignKey[i],getResources().getString(chineseSignValue[i]));
        }
        //语文声调键盘键值对
        for(int i=0;i<chineseToneKey.length;i++){
            keyboardChineseMap.put(chineseToneKey[i],getResources().getString(chineseToneValue[i]));
        }
    }


    /**
     * 初始化基本语文键盘模型(语文中的字母,符号,声调)
     **/
    private void initKeyBoardChineseView() {
        View keyboardChineseView = getLayoutView(R.layout.keyboard_chinese);
        addView(keyboardChineseView, getKeyBoardContentParams());
        chineseView = (LinearLayout) keyboardChineseView.findViewById(R.id.layout_change);
        chineseLetterCaseView = (LinearLayout) getLayoutView(R.layout.keyboard_math_letter_case);
        chineseLetterSmallView = (LinearLayout) getLayoutView(R.layout.keyboard_math_letter_small);
        chineseLetterCaseView.setWeightSum((float) 4.5);
        chineseLetterSmallView.setWeightSum((float) 4.5);
        chineseView.addView(chineseLetterSmallView, getKeyBoardContentParams());
        chineseView.addView(chineseLetterCaseView,getKeyBoardContentParams());
    }

    /** 初始化语文小写字母键盘的映射**/
    private void initKeyboardChineseLetterSmallOnClick() {
        for(int mathLetterSmallId:chineseLetterSmallKey){
            chineseLetterSmallView.findViewById(mathLetterSmallId).setOnClickListener(keyboardChineseOnClick);
        }
        chineseLetterSmallView.findViewById(R.id.tv_keyboard_delete).setOnClickListener(keyboardChineseOnClick);
        chineseLetterSmallView.findViewById(R.id.tv_keyboard_delete).setOnLongClickListener(keyboardChineseOnLongClick);
        chineseLetterSmallView.findViewById(R.id.tv_keyboard_small).setOnClickListener(keyboardChineseNavigationOnClick);
    }

    /** 初始化语文大写字母键盘的映射**/
    private void initKeyboardChineseLetterCaseOnClick(){
        for(int mathLetterCaseId:chineseLetterCaseKey){
            chineseLetterCaseView.findViewById(mathLetterCaseId).setOnClickListener(keyboardChineseOnClick);
        }
        chineseLetterCaseView.findViewById(R.id.tv_keyboard_delete).setOnClickListener(keyboardChineseOnClick);
        chineseLetterCaseView.findViewById(R.id.tv_keyboard_delete).setOnLongClickListener(keyboardChineseOnLongClick);
        chineseLetterCaseView.findViewById(R.id.tv_keyboard_case).setOnClickListener(keyboardChineseNavigationOnClick);
    }


    /**
     * 语文三种键盘类型绑定事件,对外提供给大键盘使用
     */

    OnClickListener keyboardChineseNavigationOnClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_chinese_letter:
                    if (chineseLetterSmallView == null) {
                        chineseLetterSmallView = (LinearLayout) getLayoutView(R.layout.keyboard_math_letter_small);
                    }
                    chineseView.removeAllViews();
                    chineseView.addView(chineseLetterSmallView, getKeyBoardContentParams());
                    chineseNavigationLetter.setTextColor(getResources().getColor(R.color.keyboard_white));
                    chineseNavigationSign.setTextColor(getResources().getColor(R.color.keyboard_black));
                    chineseNavigationTone.setTextColor(getResources().getColor(R.color.keyboard_white));
                    chineseNavigationLetter.setBackgroundResource(R.drawable.keyboard_item_navigation);
                    chineseNavigationSign.setBackgroundResource(R.drawable.keyboard_item_navigation_pressed);
                    chineseNavigationTone.setBackgroundResource(R.drawable.keyboard_item_navigation_pressed);
                    break;
                case R.id.tv_chinese_sign:
                    if (chineseSignView == null) {
                        chineseSignView = getLayoutView(R.layout.keyboard_chinese_sign);
                    }
                    initKeyboardChineseSignOnClick();
                    chineseView.removeAllViews();
                    chineseView.addView(chineseSignView, getKeyBoardContentParams());
                    chineseNavigationLetter.setTextColor(getResources().getColor(R.color.keyboard_white));
                    chineseNavigationSign.setTextColor(getResources().getColor(R.color.keyboard_black));
                    chineseNavigationTone.setTextColor(getResources().getColor(R.color.keyboard_white));
                    chineseNavigationLetter.setBackgroundResource(R.drawable.keyboard_item_navigation_pressed);
                    chineseNavigationSign.setBackgroundResource(R.drawable.keyboard_item_navigation);
                    chineseNavigationTone.setBackgroundResource(R.drawable.keyboard_item_navigation_pressed);
                    break;
                case R.id.tv_chinese_tone:
                    if (chineseToneView == null) {
                        chineseToneView = getLayoutView(R.layout.keyboard_chinese_tone);
                    }
                    initKeyboardChineseToneOnClick();
                    chineseView.removeAllViews();
                    chineseView.addView(chineseToneView, getKeyBoardContentParams());
                    chineseNavigationLetter.setTextColor(getResources().getColor(R.color.keyboard_white));
                    chineseNavigationSign.setTextColor(getResources().getColor(R.color.keyboard_white));
                    chineseNavigationTone.setTextColor(getResources().getColor(R.color.keyboard_black));
                    chineseNavigationLetter.setBackgroundResource(R.drawable.keyboard_item_navigation_pressed);
                    chineseNavigationSign.setBackgroundResource(R.drawable.keyboard_item_navigation_pressed);
                    chineseNavigationTone.setBackgroundResource(R.drawable.keyboard_item_navigation);
                    break;
                case R.id.tv_keyboard_small:
                    if(chineseLetterCaseView!=null){
                        chineseLetterCaseView = (LinearLayout) getLayoutView(R.layout.keyboard_math_letter_case);
                        chineseLetterCaseView.setWeightSum((float) 4.5);
                    }
                    initKeyboardChineseLetterCaseOnClick();
                    chineseView.removeAllViews();
                    chineseView.addView(chineseLetterCaseView,getKeyBoardContentParams());
                    break;
                case R.id.tv_keyboard_case:
                    if(chineseLetterSmallView!=null){
                        chineseLetterSmallView = (LinearLayout) getLayoutView(R.layout.keyboard_math_letter_small);
                        chineseLetterSmallView.setWeightSum((float) 4.5);
                    }
                    initKeyboardChineseLetterSmallOnClick();
                    chineseView.removeAllViews();
                    chineseView.addView(chineseLetterSmallView,getKeyBoardContentParams());
                    break;
            }

        }
    };

    /**
     * 公共按键(删除,每个按键的事件)数学键盘点击事件
     **/
    OnClickListener keyboardChineseOnClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (keyBoardCallBack != null) {
                switch (v.getId()) {
                    case R.id.tv_keyboard_delete:
                        keyBoardCallBack.deleteKeyBoard();
                        break;
                    case R.id.tv_keyboard_molecular:
                        keyBoardCallBack.showMolecular();
                        break;
                    default:
                        String mathNumberValue = keyboardChineseMap.get(v.getId());
                        keyBoardCallBack.getFinishEditValue(mathNumberValue);
                }
            }
        }
    };

    /**
     * 公共按键(长按删除)数学键盘长按事件
     **/
    OnLongClickListener keyboardChineseOnLongClick = new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            switch (v.getId()) {
                case R.id.tv_keyboard_delete:
                    if (keyBoardCallBack != null) {
                        keyBoardCallBack.deleteLongKeyBoard();
                    }
                    break;
            }
            return false;
        }
    };

    /**
     * 绑定导航栏的按钮
     */

    public void setNavigationSelected(View keyBoardNavigationEnglish){
        this.chineseNavigationSign = (TextView) keyBoardNavigationEnglish.findViewById(R.id.tv_chinese_sign);
        this.chineseNavigationTone = (TextView) keyBoardNavigationEnglish.findViewById(R.id.tv_chinese_tone);
        this.chineseNavigationLetter = (TextView)keyBoardNavigationEnglish.findViewById(R.id.tv_chinese_letter);
        this.chineseNavigationSign.setOnClickListener(keyboardChineseNavigationOnClick);
        this.chineseNavigationTone.setOnClickListener(keyboardChineseNavigationOnClick);
        this.chineseNavigationLetter.setOnClickListener(keyboardChineseNavigationOnClick);
    }

    /**
     * 内容区域需要的布局参数
     **/
    public RelativeLayout.LayoutParams getKeyBoardContentParams() {
        return new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    }

    /**
     * 根据布局id返回一个绘制的view
     **/
    public View getLayoutView(int resourceId) {
        return LayoutInflater.from(getContext()).inflate(resourceId, null);
    }

    /**
     * 设置语文按键的绑定事件
     **/
    public void setOnClickKeyFinishListener(KeyBoardCallBack keyBoardCallBack) {
        this.keyBoardCallBack = keyBoardCallBack;
    }
    
}
